package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity2 extends AppCompatActivity {
    EditText et1, et2;
    TextView addTv, jianTv, subTv, divTv;
    CheckBox addCb, jianCb, subCb, divCb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();
        startCal();
        addCb.setOnCheckedChangeListener((CompoundButton.OnCheckedChangeListener) this);
        jianCb.setOnCheckedChangeListener((CompoundButton.OnCheckedChangeListener) this);
        subCb.setOnCheckedChangeListener((CompoundButton.OnCheckedChangeListener) this);
        divCb.setOnCheckedChangeListener((CompoundButton.OnCheckedChangeListener) this);

    }
    public void onCheckedChange(CompoundButton buttonView,boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.cb_add:
                if (addCb.isChecked()) {
                    int i1 = Integer.parseInt(et1.getText().toString());
                    int i2 = Integer.parseInt(et2.getText().toString());
                    int res = i1+i2;
                    String msg = i1+"+"+i2+"="+res;
                    addTv.setText(msg);
                    addTv.setVisibility(View.INVISIBLE);

                }else {
                    addTv.setVisibility(View.INVISIBLE);
                }
                break;
            case R.id.cb_jian:
                if (jianCb.isChecked()) {
                    int i1 = Integer.parseInt(et1.getText().toString());
                    int i2 = Integer.parseInt(et2.getText().toString());

                }
        }
    }

    private void startCal() {
        if (addCb.isChecked()) {
            int i1 = Integer.parseInt(et1.getText().toString());
            int i2 = Integer.parseInt(et2.getText().toString());
            int res = i1+i2;
            //String msg = i1+"+"+i2"="+res;
            //  addTv.setText(msg);
            addTv.setVisibility(View.INVISIBLE);
        }else {
            addTv.setVisibility(View.INVISIBLE);
        }

    }

    private void initView() {
        et1 = findViewById(R.id.et1);
        et2 = findViewById(R.id.et2);
        addTv = findViewById(R.id.tv_add);
        jianTv = findViewById(R.id.tv_jian);
        subTv = findViewById(R.id.tv_sub);
        divTv = findViewById(R.id.tv_div);
        addCb = findViewById(R.id.cb_add);
        jianCb = findViewById(R.id.cb_jian);
        subCb = findViewById(R.id.cb_sub);
        divCb = findViewById(R.id.cb_div);

        addTv.setVisibility(View.INVISIBLE);
        jianTv.setVisibility(View.INVISIBLE);
        subTv.setVisibility(View.INVISIBLE);
        divTv.setVisibility(View.INVISIBLE);

    }
}
